package com.tc.zigbee.device.config;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlSerializer;

import com.tc.gateway.device.DeviceConfig;
import com.tc.util.DataUtil;
import com.tc.zigbee.entity.device.ZigbeeDevice;

public class DeviceConfigManager {
	static DeviceConfigManager instance;
	static{
		instance = new DeviceConfigManager();
	}
	static public DeviceConfigManager getInstance(){
		return instance;
	}
	
	Map<String, Map<String, DeviceConfig>> configs = new HashMap<String, Map<String, DeviceConfig>>();
	public void load(InputStream in) throws Exception{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(in);
		NodeList vs = doc.getElementsByTagName("node");
		for(int i = 0; i < vs.getLength(); i++){
			Element v = (Element)vs.item(i);
			String ieee = v.getAttribute("ieee");
			Map<String, DeviceConfig> node = configs.get(ieee);
			if(node == null){
				node = new HashMap<String, DeviceConfig>();
				configs.put(ieee, node);
			}
			NodeList ds = doc.getElementsByTagName("device");
			for(int j = 0; j < ds.getLength(); j++){
				Element d = (Element)ds.item(i);
				String ep = d.getAttribute("endpoint");
				DeviceConfig dc = new DeviceConfig();
				dc.setExport(Boolean.parseBoolean(d.getAttribute("export")));
				dc.setName(d.getAttribute("name"));
				node.put(ep, dc);
			}
		}
	}
	public void save(OutputStream out) throws Exception{
//		XmlSerializer serializer = ParserFactory.getSerializer();
//			serializer.setOutput(out, "UTF-8");
//			serializer.startDocument("UTF-8", true);
//			serializer.startTag("", "deviceConfigs");
//			for(String n : configs.keySet()){
//				Map<String, DeviceConfig> node = configs.get(n);
//				serializer.startTag("", "node");
//				serializer.attribute("", "ieee", n);
//				
//				for(String d : node.keySet()){
//					DeviceConfig dc = node.get(d);
//					serializer.startTag("", "device");
//					serializer.attribute("", "endpoint", d);
//					serializer.attribute("", "name", dc.getName());
//					serializer.attribute("", "export", dc.isExport() + "");
//					serializer.endTag("", "device");
//				}
//				
//				serializer.endTag("", "node");
//			}
//			serializer.endTag("", "deviceConfigs");
//			serializer.endDocument();
	}
	public void match(ZigbeeDevice d){
		String ieee = DataUtil.hex(d.getEndpoint().getParent().getIeeeAddress());
		Map<String, DeviceConfig> node = configs.get(ieee);
		if(node == null){
			node = new HashMap<String, DeviceConfig>();
			configs.put(ieee, node);
		}
		DeviceConfig dc = node.get(d.getEndpoint().getEndpointId());
		if(dc == null){
			dc = new DeviceConfig();
			node.put(DataUtil.hex(d.getEndpoint().getEndpointId()), dc);
		}
		d.setConfig(dc);
		
	}
}
